//
//  DateViewController.m
//  New
//
//  Created by dlios on 15-7-21.
//  Copyright (c) 2015年 yangTao. All rights reserved.
//

#import "DateViewController.h"

@interface DateViewController ()

@end

@implementation DateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor grayColor];
    self.view.alpha = 0.7;
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(50, self.view.frame.size.height/6, WIDTH - 100, 280)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];
    
    self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, WIDTH - 100, 280)];
    self.pickView.backgroundColor = [UIColor blackColor];
    [view addSubview:self.pickView];
    self.pickView.userInteractionEnabled = YES;
    
    
    
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 230, WIDTH - 100, 1)];
    aView.backgroundColor = [UIColor blueColor];
    [view addSubview:aView];
    BaseButton *button = [[BaseButton alloc]initWithFrame:CGRectMake(0, 230, WIDTH - 100, 50)];
    button.backgroundColor = [UIColor clearColor];
    [view addSubview:button];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 230, WIDTH - 100, 50)];
    label.text = @"确定";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:22];
    [view addSubview:label];
    label.textAlignment = NSTextAlignmentCenter;
    
    
    self.yearArray = [[NSMutableArray alloc]init];
    
    
    for (int i = 2010; i <= 2050 ; i++)
    {
        [self.yearArray addObject:[NSString stringWithFormat:@"%d",i]];
        
    }
    
    self.monthArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
    
    self.DaysArray = [[NSMutableArray alloc]init];
    
    for (int i = 1; i <= 31; i++)
    {
        [self.DaysArray addObject:[NSString stringWithFormat:@"%d",i]];
        
    }
    
    
    
    

    self.pickView.delegate = self;
    self.pickView.dataSource = self;

    
       
    // Do any additional setup after loading the view.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 3;

}

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           reusingView:(UIView *)view{
    
    UILabel *pickerLabel = (UILabel *)view;
    if (pickerLabel == nil) {
        CGRect frame = CGRectMake(0.0, 0.0, 50, 60);
        pickerLabel = [[UILabel alloc] initWithFrame:frame];
        
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor blackColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:15.0f]];
        pickerLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0];
        pickerLabel.textColor = [UIColor greenColor];
        
    }
    if (component == 0)
    {
        pickerLabel.text =  [self.yearArray objectAtIndex:row]; // Year
    }
    else if (component == 1)
    {
        pickerLabel.text =  [self.monthArray objectAtIndex:row];  // Month
    }
    else if (component == 2)
    {
        pickerLabel.text =  [self.DaysArray objectAtIndex:row]; // Date
        
    }
    
    return pickerLabel;
    
}



-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0)
    {
        return [self.yearArray count];
        
    }
    else if (component == 1)
    {
        return [self.monthArray count];
    }
    else if (component == 2)
    { // day
        
        if (self.firstTimeLoad == YES)
        {
            NSInteger currentMonth = [self.currentMonthString integerValue];
            if (currentMonth == 1 || currentMonth == 3 || currentMonth == 5 || currentMonth == 7 || currentMonth == 8 || currentMonth == 10 || currentMonth == 12)
            {
                return 31;
            }
            else if (currentMonth == 2)
            {
                int yearint = [[self.yearArray objectAtIndex:self.selectedYearRow]intValue ];
                
                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
                    
                    return 29;
                }
                else
                {
                    return 28; // or return 29
                }
                
            }
            else
            {
                return 30;
            }
            
        }
        else
        {
            
            if (self.selectedMonthRow == 0 || self.selectedMonthRow == 2 || self.selectedMonthRow == 4 || self.selectedMonthRow == 6 || self.selectedMonthRow == 7 || self.selectedMonthRow == 9 || self.selectedMonthRow == 11)
            {
                return 31;
            }
            else if (self.selectedMonthRow == 1)
            {
                int yearint = [[self.yearArray objectAtIndex:self.selectedYearRow]intValue ];
                
                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
                    return 29;
                }
                else
                {
                    return 28; // or return 29
                }
                
                
                
            }
            else
            {
                return 30;
            }
            
        }
        
    }
    return 1;



}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if (component == 0) {
        self.year = [self.yearArray objectAtIndex:row];
    
    }if (component == 1) {
        self.mouth = [self.monthArray objectAtIndex:row];
        
    }else if (component == 2){
        self.date = [self.DaysArray objectAtIndex:row];
    }


}



- (void)buttonAction{
  
    
    [self.myDalegate bringButtonyear:self.year AndMouth:self.mouth andDate:self.date];
//
////    calenderDetailViewController *detailVC = [[calenderDetailViewController alloc]init];
// 
//    if ((self.year == nil) && (self.mouth == nil) && (self.date == nil)) {
//        NSDate *date = [NSDate date];
//        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//        [formatter setDateFormat:@"yyyy"];
//        NSString *str1 =[NSString stringWithFormat:@"%@",
//                           [formatter stringFromDate:date]];
//        
//        [formatter setDateFormat:@"MM"];
//        NSString *string1 =[NSString stringWithFormat:@"%02ld",[[formatter stringFromDate:date]integerValue]];
//        
//        [formatter setDateFormat:@"dd"];
//        NSString *string2 =[NSString stringWithFormat:@"%02ld",[[formatter stringFromDate:date] integerValue]];
//        NSString *string3 = [NSString stringWithFormat:@"%02ld",[[formatter stringFromDate:date] integerValue]-1];
//
//        
////        detailVC.date = [NSString stringWithFormat:@"%@-%@-%@",str1,string1,string2];
////        detailVC.date1 = [NSString stringWithFormat:@"%@-%@-%@",str1,string1,string3];
////[detailVC getInFoDate:detailVC.date  andDate1: detailVC.date1];
//        [self.mydelegate bringDate:[NSString stringWithFormat:@"%@-%@-%@",str1,string1,string2] andDate1:[NSString stringWithFormat:@"%@-%@-%@",str1,string1,string3]];
//        
//    }else if ((self.year == nil) && (self.mouth != nil) && (self.date != nil)){
//        NSDate *date = [NSDate date];
//        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//        [formatter setDateFormat:@"yyyy"];
//        NSString *str2 =[NSString stringWithFormat:@"%@",
//                         [formatter stringFromDate:date]];
//        NSInteger num = [self.mouth integerValue];
//        NSInteger num1 = [self.date integerValue];
//        
//        NSString *str = [NSString stringWithFormat:@"%@-%02ld-%02ld",str2,num,num1];
//        NSString *str1 = [NSString stringWithFormat:@"%@-%02ld-%02ld",str2,num,num1-1];
////        detailVC.date = str;
////        detailVC.date1 = str1;
////        [detailVC getInFoDate:str andDate1:str1];
//        
//        [self.mydelegate bringDate:str andDate1:str1];
//        
//    
//    }else if ((self.year == nil) && (self.mouth == nil) && (self.date != nil)){
//        NSDate *date = [NSDate date];
//        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//        [formatter setDateFormat:@"yyyy"];
//        NSString *str2 =[NSString stringWithFormat:@"%@",
//                         [formatter stringFromDate:date]];
//        
//        [formatter setDateFormat:@"MM"];
//        NSString *string1 =[NSString stringWithFormat:@"%02ld",[[formatter stringFromDate:date]integerValue]];
//        
//        NSInteger num1 = [self.date integerValue];
//        
//        NSString *str = [NSString stringWithFormat:@"%@-%@-%02ld",str2,string1,num1];
//        NSString *str1 = [NSString stringWithFormat:@"%@-%@-%02ld",str2,string1,num1-1];
////        detailVC.date = str;
////        detailVC.date1 = str1;
////[detailVC getInFoDate:str andDate1:str1];
//        
//        [self.mydelegate bringDate:str andDate1:str1];
//    
//    }
//    
//    else{
//        
//        NSInteger num = [self.mouth integerValue];
//        NSInteger num1 = [self.date integerValue];
//        
//        NSString *str = [NSString stringWithFormat:@"%@-%02ld-%02ld",self.year,num,num1];
//        NSString *str1 = [NSString stringWithFormat:@"%@-%02ld-%02ld",self.year,num,num1-1];
////        detailVC.date = str;
////        detailVC.date1 = str1;
////    [detailVC getInFoDate:str andDate1:str1];
//        
//        [self.mydelegate bringDate:str andDate1:str1];
//    }
//
  
//    [detailVC.array removeAllObjects];
//    [detailVC.tableV reloadData];

}

- (void)viewDidAppear:(BOOL)animated
{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    
    
    NSString *currentyearString = [NSString stringWithFormat:@"%@",
                                   [formatter stringFromDate:date]];
    
    
    
    [formatter setDateFormat:@"MM"];
    
    self.firstTimeLoad = YES;
    
    self.currentMonthString = [NSString stringWithFormat:@"%ld",[[formatter stringFromDate:date]integerValue]];
    
    
    // Get Current  Date
    
    [formatter setDateFormat:@"dd"];
    NSString *currentDateString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    [self.pickView selectRow:[self.yearArray indexOfObject:currentyearString] inComponent:0 animated:YES];
    
    [self.pickView selectRow:[self.monthArray indexOfObject:self.currentMonthString] inComponent:1 animated:YES];

    
    [self.pickView selectRow:[self.DaysArray indexOfObject:currentDateString] inComponent:2 animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
